package main

import (
	"fmt"
	"os"
	"text/template"
)

/*
func main() {
	// 解析模板 字符串
	text := "我的名字叫{{.}}\n"
	tpl, err := template.New("test").Parse(text)
	if err != nil {
		fmt.Println(err)
	}
	tpl.Execute(os.Stdout, "sonfer")

	tpl.Execute(os.Stdout, 1)
	tpl.Execute(os.Stdout, true)
	tpl.Execute(os.Stdout, []int{1, 2, 3})
	tpl.Execute(os.Stdout, map[int]int{1: 1, 2: 2, 3: 3})

	// 判断
	text = "性别：{{if .}}男{{else}}女{{end}}\n"
	tpl, _ = template.New("test").Parse(text)
	tpl.Execute(os.Stdout, true)

	// 判断
	// gt >
	// lt <
	// gte >=
	// lte <=
	// eq =
	// neq !=
	text = "性别：{{if eq 1 .}}男{{else}}女{{end}}\n"
	tpl, _ = template.New("test").Parse(text)
	tpl.Execute(os.Stdout, 0)
	// {{range .}}是表示外部传进来的字符串切片
	// {{.}} 表示遍历到的元素
	text = "学生列表：{{range .}} {{.}} {{end}}\n"
	tpl, _ = template.New("test").Parse(text)
	tpl.Execute(os.Stdout, []string{"张三", "李四", "王二"})

	text = "第一个元素: {{index . 0}}\n"
	tpl, _ = template.New("test").Parse(text)
	tpl.Execute(os.Stdout, []string{"张三", "李四", "王二"})

	text = "map--- name: {{.name}} addr: {{.addr}}\n"
	tpl, _ = template.New("test").Parse(text)
	tpl.Execute(os.Stdout, map[string]string{"name": "xxxx", "addr": "yyyy"})

	text = "struct--- name: {{.Name}} addr: {{.Addr}}\n"
	tpl, _ = template.New("test").Parse(text)
	tpl.Execute(os.Stdout, struct{ Name, Addr string }{"xx", "yy"})
}
*/

func main() {
	tpl, err := template.ParseFiles("./user.html")
	if err != nil {
		fmt.Println(err)
	}

	tpl.ExecuteTemplate(os.Stdout, "user.html", struct{ Name, Addr string }{"xxx", "yyy"})
}
